// 这里配置mockjs 数据 模拟真实的后端请求 用法再后端数据接口未完成 前端可以快速开发

module.exports = {
    configureWebpack: {
        devServer: {
            // mockjs编写数据的地方 
            before(app) {
                // 全部用户列表 
                var userlist = [{
                    "id": 10,
                    "UserName": "zs1",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "18888888888",
                    "Email": "405328555@qq.com",
                    "MgState": false
                }, {
                    "id": 9,
                    "UserName": "zs2",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "18888888888",
                    "Email": "405328555@qq.com",
                    "MgState": true
                }, {
                    "id": 8,
                    "UserName": "zs3",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "18888888888",
                    "Email": "405328555@qq.com",
                    "MgState": true
                }, {
                    "id": 7,
                    "UserName": "zs4",
                    "Password": "555555",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13312311231",
                    "Email": "55@qq.com",
                    "MgState": true
                }, {
                    "id": 6,
                    "UserName": "zs5",
                    "Password": "333333",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13388888888",
                    "Email": "33@qq.com",
                    "MgState": true
                }, {
                    "id": 5,
                    "UserName": "zs6",
                    "Password": "222222",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13845644564",
                    "Email": "22@qq.com",
                    "MgState": true
                }, {
                    "id": 4,
                    "UserName": "zs7",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "",
                    "Email": "",
                    "MgState": true
                }, {
                    "id": 3,
                    "UserName": "zs8",
                    "Password": "",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13812341235",
                    "Email": "888888@qq.com",
                    "MgState": true
                }, {
                    "id": 2,
                    "UserName": "zs9",
                    "Password": "",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13812341888",
                    "Email": "288888@qq.com",
                    "MgState": true
                }, {
                    "id": 1,
                    "UserName": "zs10",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "超级管理员",
                    "Mobile": "13602221234",
                    "Email": "223456@qq.com",
                    "MgState": true
                }]


                app.get('/api/users',(req,res)=>{
                    const {query,pagenum,pagesize} = req.query 
                    var total = userlist.length
                    // var newdatalist = userlist.slice(pagenum*pagesize,(parseInt(pagenum)+1)*pagesize)
                    var newdatalist = userlist.slice((parseInt(pagenum)-1)*pagesize,pagenum*pagesize)

                    res.json({
                        'status':'ok',
                        'total':total,
                        'data':newdatalist
                    })
                })




                app.get('/api/menu', (req, res) => {
                    res.json({

                        "status": "ok",
                        "data": [{
                            "id": 1,
                            "name": "用户管理",
                            "child": [{
                                "id": 6,
                                "name": "用户列表",
                                "path": "/home/users"
                            }]
                        }, {
                            "id": 2,
                            "name": "员工管理",
                            "child": [{
                                "id": 7,
                                "name": "员工列表",
                                "path": "/home/rote"
                            },  ]
                        }, {
                            "id": 3,
                            "name": "商品管理",
                            "child": [{
                                "id": 8,
                                "name": "商品列表",
                                "path": "/home/product"
                            }, {
                                "id": 9,
                                "name": "分类参数",
                                "path": "/home/userss"
                            }, {
                                "id": 10,
                                "name": "商品分类",
                                "path": "/home/producttype"
                            }]
                        }, {
                            "id": 4,
                            "name": "订单管理",
                            "child": []
                        }, {
                            "id": 5,
                            "name": "数据统计",
                            "child": []
                        }]
                    })
                })








                // 模拟写一个登录接口 
                const token = 'tokendfdf'
                app.get('/api/login', (req, res) => {
                    const { username, password } = req.query
                    // 这里模拟数据库
                    if (username == 'admin' && password == '123456') {
                        res.json({
                            code: 1,
                            message: '登录成功',
                            token: token + username + 'suijishu'
                        })
                    } else {
                        res.json({
                            code: 0,
                            message: '登录失败',

                        })
                    }
                })



                // 点击改变用户状态按钮 接口
                app.get('/api/userupdate',(req,res)=>{
                    var {id,MgState} = req.query 
                    var user = userlist.filter(u=>u.id==id)
                    // 这个user是数据库找到的哪一项 
                    user.MgState = MgState
                    


                    res.json ({
                        "code":"200",
                        "newState":user.MgState
                    })
                })


                // 搜索接口 
                app.get('/api/users2',(req,res)=>{
                    const {query} = req.query 

                    var newdatalist = [] 
                    if(query !='' ) {
                        newdatalist=userlist.filter(u=>u.UserName==query)
                    }

                    res.json({
                        'status':'ok',
                        'data':newdatalist
                    })
                })


                // 添加用户接口 
                // mock.js添加用户的接口
                // addForm: {
                //     username: "",
                //     password: "",
                //     email: "",
                //     mobile: "",
                //     pagenum: 1,
                //     pagesize: 3
                //   },

                app.get('/api/adduser', (req, res) => {
                    const { username, password, email, mobile,   pagenum, pagesize } = req.query
                    var id = userlist[0].id
                    var adduser = {
                        "id": id + 1,
                        "UserName": username,
                        "Password": password,
                        "CreateTime": "0001/1/1 星期一 0:00:00",
                        "RoleName": "",
                        "Mobile": mobile,
                        "Email": email,
                        "MgState": true
                    }
                    userlist.unshift(adduser)
                    
                    total = userlist.length
                    var newDataList = []
                   
                        newDataList = userlist.slice((parseInt(pagenum) - 1) * pagesize, pagenum * pagesize)
                        //向前端反馈的数据
                    res.json({
                        'total': total,
                        'data': newDataList
                    })
                })




           
                  // 修改用户接口  
                //   updateForm: {
                    // id:'10'
                //     username: "吴亦凡",
                //     password: "",
                //     email: "",
                //     mobile: "",
                //        // pagenum: 1,
                //     // pagesize: 3
                //   },
                  app.get('/api/updateuser', (req, res) => {
                    const { id, username, password, email, mobile,pagenum, pagesize } = req.query
                    // {
                    //     "id": 10,
                    //     "UserName": "zs1",
                    //     "Password": "123456",
                    //     "CreateTime": "0001/1/1 星期一 0:00:00",
                    //     "RoleName": "",
                    //     "Mobile": "18888888888",
                    //     "Email": "405328555@qq.com",
                    //     "MgState": false
                    // }
                        userlist.filter(u => u.id == id).forEach(element => {
                            element.UserName = username
                            element.Email = email
                            element.Mobile = mobile
                            element.Password = password
                        })
                    
                    var total = userlist.length
                 
                        newDataList = userlist.slice((parseInt(pagenum) - 1) * pagesize, pagenum * pagesize)

                    res.json({
                        'status': 'ok',
                        'total': total,
                        'data': newDataList
                    })
                })
            }
        }
    }
}